package com.sscaipiao.android.utils;

import java.io.IOException;
import java.util.ArrayList;

import android.widget.Toast;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;

public class UserDataServiceHelper {
	
	public static String getForgetPassword(Context context, String phone, String rand){
		String uri = Constants.SERVER_URL + "getForgetPassword.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		//pairs.add(new BasicNameValuePair("token",user.getToken()));
		pairs.add(new BasicNameValuePair("uid",phone));
		pairs.add(new BasicNameValuePair("rand",rand));
		
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("getForgetPassword", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE ;
			//user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.getForgetPassword", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.getForgetPassword", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}	
	
	public static String saveNewPassword(Context context, User user, String newPassword){
		String uri = Constants.SERVER_URL + "modifyPassword.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token",user.getToken()));
		pairs.add(new BasicNameValuePair("oldPassword",StringUtil.str2MD5( user.getPassword())));
		pairs.add(new BasicNameValuePair("newPassword",StringUtil.str2MD5( newPassword)));
		
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("saveNewPassword", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE ;
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.saveNewPassword", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.saveNewPassword", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}
	
	public static String saveUserInfo(Context context, User user){
		String uri = Constants.SERVER_URL + "saveUserIdentify.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token",user.getToken()));
		pairs.add(new BasicNameValuePair("name",user.getName()));
		pairs.add(new BasicNameValuePair("idCard",user.getIdCard()));
		pairs.add(new BasicNameValuePair("email",user.getEmail()));
		pairs.add(new BasicNameValuePair("md5Password",StringUtil.str2MD5( user.getPassword())));
		
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("saveUserInfo", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE ;
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.saveUserInfo", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.saveUserInfo", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}
	
	public static String getBankInfo(Context context, User user){
		String uri = Constants.SERVER_URL + "getBankInfo.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token",user.getToken()));
		
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE_COMPLEX + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE_COMPLEX +
					jsonObject.getString(Constants.BANK);
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.getBankInfo", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.getBankInfo", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}	
	
	public static String saveBankInfo(Context context, User user, String province, String city){
		String uri = Constants.SERVER_URL + "saveBankInfo.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token",user.getToken()));
		pairs.add(new BasicNameValuePair("bankno",user.getBankno()));
		pairs.add(new BasicNameValuePair("bank",user.getBank()));
		pairs.add(new BasicNameValuePair("bankFullName",user.getBankFullName()));
		pairs.add(new BasicNameValuePair("province",province));
		pairs.add(new BasicNameValuePair("city",city));
		pairs.add(new BasicNameValuePair("md5Password",StringUtil.str2MD5( user.getPassword())));
		
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("saveBankInfo", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE ;
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.saveBankInfo", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.saveBankInfo", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}
	
	public static String addComment(Context context, String comment, User user){
		String uri = Constants.SERVER_URL + "addComment.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token",user.getToken()));
		pairs.add(new BasicNameValuePair("comment",comment));
		
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE ;
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.addComment", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.addComment", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}
	
	public static String getSubBank(Context context, String bankName, String subBankName, User user){
		String uri = Constants.SERVER_URL + "getSubBankList.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("bankName",bankName));
		pairs.add(new BasicNameValuePair("subBankName",subBankName));
		
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE +  
					jsonObject.getString(Constants.SUB_BANK_LIST) ;
			//user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}
	
	public static String getCity(Context context, String provName, User user){
		String uri = Constants.SERVER_URL + "getCityList.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("provName",provName));
		
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE +  
					jsonObject.getString(Constants.CITY_LIST) ;
			//user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}
	
	public static String getProvince(Context context,  User user){
		String uri = Constants.SERVER_URL + "getProvList.do";
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(new ArrayList<BasicNameValuePair>(), uri);
			
			JSONObject jsonObject = new JSONObject(responseStr);
			Log.v("getProvince", responseStr);
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE +  
					jsonObject.getString(Constants.PROV_LIST) ;
			//user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}
	
	public static String getBank(Context context, User user){
		String uri = Constants.SERVER_URL + "getBankList.do";
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(new ArrayList<BasicNameValuePair>(), uri);
			
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE_COMPLEX + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE_COMPLEX +  
					jsonObject.getString(Constants.BANK_LIST) ;
			
			//user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}
	
	public static String getValidateCode(Context context, String phone, User user){
		String uri = Constants.SERVER_URL + "getMobileRand.do?mobile="+phone;
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(new ArrayList<BasicNameValuePair>(), uri);
			
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE +  
					//jsonObject.getString(Constants.TOKEN) + Constants.STRING_SPLITE +  
					jsonObject.getString(Constants.RAND) ;
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.v("UserDataServiceHelper.getValidateCode", e.getLocalizedMessage());
			e.printStackTrace();
		}

		return retStr;
	}
	
	public static User  login(Context context, String account , String password) throws IOException{
		User user = null;
		String uri = Constants.SERVER_URL + "login.do";

		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("uid",account));
		pairs.add(new BasicNameValuePair("password",password));
		NetworkManager nm = new NetworkManager(context);
		String responseStr = nm.SendAndWaitResponse(pairs, uri);
//				"{\"strMessage\":\"登录成功\",\"token\":\"2F89958817017273F74FE15497C148033DD63F93C0CE08563C126442F1CDA2DC\"," +
//				"\"status\":\"01\",\"user\":{\"uid\":\"13661975336\",\"idCard\":\"\",\"bankno\":" +
//				"\"12345678864343\",\"answer\":\"china\",\"city\":\"崇文区\",\"point\":2,\"balance\":" +
//				"\"0.00\",\"level\":0,\"email\":\"test@163.com\",\"name\":\"\",\"province\":\"北京市\"," +
//				"\"bank\":\"中国人民银行 \",\"questionType\":\"1000\",\"question\":\"\",\"bankFullName\":\"123\"}}";

		Log.v("UserDataServiceHelper.login", "response="+responseStr);
		if( StringUtil.isEmptyString(responseStr)) {
			return user;
		}
		try {
			JSONObject jsonObject = new JSONObject(responseStr);
			String status = jsonObject.getString(Constants.STATUS);
			if(status.contains("03")){
				Toast.makeText(context,responseStr,Toast.LENGTH_SHORT).show();
				return user ;
			}
			JSONObject userJson = jsonObject.getJSONObject(Constants.USER);
			user = new User();
			user.setMessage(jsonObject.getString(Constants.MESSAGE));
			user.setStatus(jsonObject.getString(Constants.STATUS));
			user.setName(userJson.isNull(Constants.NAME) ? "":userJson.getString(Constants.NAME));
			user.setBank(userJson.isNull(Constants.BANK) ? "":userJson.getString(Constants.BANK));
			user.setIdCard(userJson.isNull(Constants.ID_CARD) ? "":userJson.getString(Constants.ID_CARD));
			user.setBankno(userJson.isNull(Constants.BANK_NO) ? "":userJson.getString(Constants.BANK_NO));
			user.setCity(userJson.isNull(Constants.CITY) ? "":userJson.getString(Constants.CITY));
			user.setProvince(userJson.isNull(Constants.PROVINCE) ? "":userJson.getString(Constants.PROVINCE));
			user.setEmail(userJson.isNull(Constants.EMAIL) ? "":userJson.getString(Constants.EMAIL));
			user.setBankFullName(userJson.isNull(Constants.BANK_FULL_NAME) ? "":userJson.getString(Constants.BANK_FULL_NAME));
			user.setBalance(userJson.isNull(Constants.BALANCE) ? "":userJson.getString(Constants.BALANCE));
			if(user.getStatus().equals(Constants.LOGIN_SUCCESS_CODE)){
				user.setToken(jsonObject.getString(Constants.TOKEN));
			}
			user.setPassword(password);
			user.setAccount(account);
		} catch (JSONException e) {

			e.printStackTrace();
		}

		
		return user;
		
	}

	public static User  register(Context context, User user) throws IOException{
		String uri = Constants.SERVER_URL + "regUser.do";//?account="+account+"&password="+password+"&name="+name;

		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("uid",user.getAccount()));
		pairs.add(new BasicNameValuePair("password",user.getPassword()));
		pairs.add(new BasicNameValuePair("rePassword",user.getPassword()));
		pairs.add(new BasicNameValuePair("questionType",user.getQuestionType()));
		pairs.add(new BasicNameValuePair("question",user.getQuestion()));
		pairs.add(new BasicNameValuePair("answer",user.getAnswer()));
		pairs.add(new BasicNameValuePair("email",user.getEmail()));
		pairs.add(new BasicNameValuePair("name",user.getName()));
		pairs.add(new BasicNameValuePair("idCard",user.getIdCard()));
		pairs.add(new BasicNameValuePair("bank",user.getBank()));
		pairs.add(new BasicNameValuePair("province",user.getProvince()));
		pairs.add(new BasicNameValuePair("city",user.getCity()));
		pairs.add(new BasicNameValuePair("bankFullName",user.getBankFullName()));
		pairs.add(new BasicNameValuePair("bankno",user.getBankno()));
		pairs.add(new BasicNameValuePair("rand",user.getRand()));
		
		NetworkManager nm = new NetworkManager(context);
		String responseStr = nm.SendAndWaitResponse(pairs, uri);
		if( StringUtil.isEmptyString(responseStr)) {
			return user;
		}
		try {
			JSONObject jsonObject = new JSONObject(responseStr);
			user.setStatus(jsonObject.getString(Constants.STATUS));
			user.setMessage(jsonObject.getString(Constants.MESSAGE));
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (JSONException e) {

			e.printStackTrace();
		}

		
		return user;
		
	}
	
	public static  String getServerVerCode(Context context) {
		String uri = Constants.SERVER_URL + "getApk.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("type","Android"));
		
		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("UserDataServiceHelper.getServerVerCode", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);
			
			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE_COMPLEX + 
					jsonObject.getString(Constants.MESSAGE) + Constants.STRING_SPLITE_COMPLEX +
					jsonObject.getString(Constants.APKINFO);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return retStr;
	}
}
